package Tetris.Interfaz;

/** @author Adrian Garcia Rojas
  * @author Aida Ruiz Maya
  * @author HÃ©ctor Daniel GonzÃ¡lez TerÃ¡n
  * @version 1.0 30/12/2010
  * Esta programa es un tetris para el proyecto final de ICC1
  * Profesora de Asignatura: Fernanda Sanchez Puig
  * Ayudante de Laboratorio: Manuel AlcÃ¡ntara JuÃ¡rez
  */

import Tetris.Logica.Tablero;
import Tetris.Tetris;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.Timer;

public class Ventana extends javax.swing.JFrame implements ActionListener,Runnable{

    public static Timer timer;
    private Tetris juego;
     /**
     * Este constructor inicaliza el objeto timer, qe tiene como escucha al objeto ventana y tiene delay de 500 mseg.
     * Crea al Objeto juego de la clase tetris.
     * Manda a llamar el metodo initComponets()
     * El panel manda solo escucha al teclado mediante el metodo addKeyListener
     */
    public Ventana() {
        timer=new Timer(500,this);
        juego=new Tetris();
        initComponents();
        panel.setFocusable(true);
          panel.addKeyListener(new KeyAdapter(){
            @Override
            public void keyPressed(KeyEvent evt) {
                juego.keyPressed(evt);
            }
        });
        setVisible(true);
    }

    /**
     * Este metodo devuelve el Objeto timer
     * @return timer
     */

    public static Timer getTimer(){
        return timer;
    }

     /**
     * Este metodo crea el objeto Canvas que es un BufferedImage, que se le pasa como parametros el tamaÃ±o del tablero y el tipo de imagen que va a ser.
     * Crea pinzel que es un objeto de Graphics.
     * Manda a llamar mediante el objeto juego de la clase Tetris el metodo update, el metodo draw y le pasa como parametro el objeto pinzel.
     * Inserta como icono con el Objeto ImageIcon a canvas y lo reproduce en nuestra ventan, ademas del score.
     * @param e
     */

    public void actionPerformed(ActionEvent e) {
        BufferedImage canvas=new BufferedImage(Tetris.tamPixel*Tablero.X_LENGTH,Tetris.tamPixel*Tablero.Y_LENGTH,BufferedImage.TYPE_INT_RGB);
        Graphics pinzel=canvas.getGraphics();
        juego.update();
        juego.draw(pinzel);
        panel.setIcon(new ImageIcon(canvas));
        score.setText(juego.getPuntuacion()+"");
        combo.setText(juego.getCombo()+"");
        frase.setText(juego.frase);
        lineas.setText(juego.getLinea()+"");
    }

     /**
     * Empieza a correr el objeto Timer con el delay que se prestablecio
     */

    public void run() {
        timer.start();
    }

    @SuppressWarnings("unchecked")
     /**
     * Inicializa y Muestra la ventana
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panel = new javax.swing.JLabel();
        textScore = new javax.swing.JLabel();
        score = new javax.swing.JLabel();
        textCombo = new javax.swing.JLabel();
        combo = new javax.swing.JLabel();
        frase = new javax.swing.JLabel();
        textLineas = new javax.swing.JLabel();
        lineas = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Tetris");
        setResizable(false);

        textScore.setFont(new java.awt.Font("Book Antiqua", 1, 14));
        textScore.setText("Score:");

        score.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        score.setText("0");

        textCombo.setFont(new java.awt.Font("Book Antiqua", 1, 14));
        textCombo.setText("Combo:");

        combo.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        combo.setText("0");

        frase.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        frase.setText("0");

        textLineas.setFont(new java.awt.Font("Book Antiqua", 1, 14));
        textLineas.setText("Lineas:");

        lineas.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lineas.setText("0");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(combo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(textScore)
                    .addComponent(textCombo)
                    .addComponent(score, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(frase, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(textLineas)
                    .addComponent(lineas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, 600, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(textScore)
                        .addGap(18, 18, 18)
                        .addComponent(score)
                        .addGap(75, 75, 75)
                        .addComponent(textCombo)
                        .addGap(18, 18, 18)
                        .addComponent(combo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(frase)
                        .addGap(96, 96, 96)
                        .addComponent(textLineas)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lineas)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel combo;
    private javax.swing.JLabel frase;
    private javax.swing.JLabel lineas;
    private javax.swing.JLabel panel;
    private javax.swing.JLabel score;
    private javax.swing.JLabel textCombo;
    private javax.swing.JLabel textLineas;
    private javax.swing.JLabel textScore;
    // End of variables declaration//GEN-END:variables
}
